﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using System.IO;

namespace DARE
{
    public class CEffectTextures
    {
        #region Fields

        #region CONST

        public const string EFFECTTEXTURES = "TexturesMaterial";

        private const string TEXTURE = "Texture";
        private const string SPECULAR = "Specular";
        private const string BUMP = "bump";

        #endregion

        [ContentSerializer]
        private Dictionary<string, string> m_texturesName;

        [ContentSerializerIgnore]
        private Dictionary<string, Texture2D> m_textures;

        [ContentSerializer]
        private Dictionary<string, object> m_paramsForgotten;

        #endregion

        #region Property

        [ContentSerializerIgnore]
        public Texture2D Bump
        {
            get
            {
                return GetEffectTexture(BUMP);
            }
        }

        [ContentSerializerIgnore]
        public Texture2D Specular
        {
            get
            {
                return GetEffectTexture(SPECULAR);
            }
        }

        [ContentSerializerIgnore]
        public Texture2D Texture
        {
            get
            {
                return GetEffectTexture(TEXTURE);
            }
        }

        [ContentSerializerIgnore]
        public object this[string name]
        {
            get
            {
                if (m_paramsForgotten.ContainsKey(name))
                    return m_paramsForgotten[name];
                if (m_textures.ContainsKey(name))
                    return m_textures[name];
                return null;
            }
        }

        //
        // Summary:
        //     Gets or sets the diffuse color property.
        public Vector3 DiffuseColor { get; set; }

        //
        // Summary:
        //     Gets or sets the emissive color property.
        public Vector3 EmissiveColor { get; set; }

        public float Alpha { get; set; }

        //
        // Summary:
        //     Gets or sets the specular color property.
        public Vector3 SpecularColor { get; set; }

        //
        // Summary:
        //     Gets or sets the specular power property.
        public float SpecularPower { get; set; }

        //
        // Summary:
        //     Gets or sets the vertex color property.
        public bool VertexColorEnabled { get; set; }

        public string LoadPath { get; set; }

        #endregion

        #region CTOR

        public CEffectTextures()
        {
            m_textures = new Dictionary<string, Texture2D>();
            m_texturesName = new Dictionary<string, string>();
            m_paramsForgotten = new Dictionary<string, object>();
            
            LoadPath = "";

            #region DefaultParam

            Alpha = 1.0f;
            DiffuseColor = Vector3.Zero;
            EmissiveColor = Vector3.Zero;
            SpecularColor = Vector3.Zero;
            SpecularPower = 0.0f;
            VertexColorEnabled = true;

            #endregion
        }

        #endregion

        #region Method

        public bool AddTexture(string name, string _textureName)
        {
            if (m_textures.ContainsKey(name))
                return false;
            m_texturesName.Add(name, _textureName);
            return true;
        }

        private Texture2D GetEffectTexture(string name)
        {
            if (m_textures.ContainsKey(name))
                return m_textures[name];
            if (m_texturesName.ContainsKey(name))
            {
                try
                {
                    m_textures.Add(name, CResourceMgr.Load<Texture2D>(Path.Combine(LoadPath, this.m_texturesName[name])));
                }
                catch (Exception e)
                {
                    m_textures.Remove(name);
                    return null;
                }
                return this.m_textures[name];
            }
            return null;
        }

        public bool SetParam(string name, object value)
        {
            if (m_paramsForgotten.ContainsKey(name))
                return false;
            m_paramsForgotten.Add(name, value);
            return true;
        }
        #endregion
    }
}
